------------------------------------------------------------
---- EXECUTE ---> MEMORY REGISTER
------------------------------------------------------------

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity EX_MEN_REG is
	PORT (
	
	CLK : in STD_LOGIC;
	
	-- CONTROL signals
	BRANCH_FLAG_IN : in STD_LOGIC; -- Did the previous result give the green light for branching?
	MEM_READ_IN : in STD_LOGIC; -- Is this a mem read statement ?
	MEM_WRITE_IN : in STD_LOGIC; -- Is this a memb write statement?
	MEM_TO_REG_IN : in STD_LOGIC; -- Is this a load word operation? 

	-- DATA signals
	BRANCH_IN : in STD_LOGIC; -- Is this a branch statement ? 
	ALU_IN : in STD_LOGIC_VECTOR(31 downto 0); -- Information from ALU, either used as address or passed on to next stage
	WRITE_DATA_IN : in STD_LOGIC_VECTOR(31 downto 0); -- Data to be written to memory, eg for lw
	REG_DEST_IN : in STD_LOGIC_VECTOR(4 downto 0); -- Destination register (after operation, or lw)
	BRANCH_PC_IN : in STD_LOGIC_VECTOR(31 downto 0); -- New branch'ed PC
	
	-- OUTPUT signals
	BRANCH_FLAG_OUT : out STD_LOGIC;
	MEM_READ_OUT : out STD_LOGIC;
	MEM_WRITE_OUT : out STD_LOGIC;
	MEM_TO_REG_OUT : out STD_LOGIC; -- Is this a load word operation? 
	
	BRANCH_PC_OUT : out STD_LOGIC_VECTOR(31 downto 0); -- New PC if there is a branch
	BRANCH_OUT : out STD_LOGIC; -- Is this a branch instruction?
	ALU_OUT : out STD_LOGIC_VECTOR(31 downto 0);
	WRITE_DATA_OUT : out STD_LOGIC_VECTOR(31 downto 0);
	REG_DEST_OUT : out STD_LOGIC_VECTOR(4 downto 0)
	);
end EX_MEN_REG;

architecture Behavioral of EX_MEN_REG is	
begin
	process (CLK) 
	begin
	
		if CLK'event and CLK = '1' then
			-- Control related outputs
			BRANCH_FLAG_OUT <= BRANCH_FLAG_IN;
			MEM_READ_OUT <= MEM_READ_IN;
			MEM_WRITE_OUT <= MEM_WRITE_IN;
			MEM_TO_REG_OUT <= MEM_TO_REG_IN;
		
			-- Data related outputs 
			BRANCH_PC_OUT <= BRANCH_PC_IN;
			BRANCH_OUT <= BRANCH_IN;
			ALU_OUT <= ALU_IN;
			WRITE_DATA_OUT <= WRITE_DATA_IN;
			REG_DEST_OUT <= REG_DEST_IN;
		end if;
	end process;

end Behavioral;

